{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 一个线性回归模型\n",
    "\n",
    "一个直线 y = wx+b ,在这个线周围生成一些数据点，然后用TF对这些点做回归，求w和b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.PathCollection at 0x1c5d8ac92b0>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成数据\n",
    "num_points = 1000\n",
    "vectors = []\n",
    "for i in range(num_points):\n",
    "    x = np.random.normal(0.5,1.5)\n",
    "    y = 0.1*x + 0.3 + np.random.normal(0.02,0.03)\n",
    "    vectors.append([x,y])\n",
    "\n",
    "# generate some samples\n",
    "x_data = [v[0] for v in vectors]\n",
    "y_data = [v[1] for v in vectors]\n",
    "\n",
    "plt.scatter(x_data,y_data,c='r')\n",
    "# plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "W: [ 0.09163189] b: [ 0.] loss:  0.105653\n"
     ]
    }
   ],
   "source": [
    "# init W, b\n",
    "W = tf.Variable(tf.random_uniform([1],-1.,1.),name='W')\n",
    "b = tf.Variable(tf.zeros([1]),name='b')\n",
    "y = W*x_data+b\n",
    "loss = tf.reduce_mean(tf.square(y-y_data))\n",
    "\n",
    "optim = tf.train.GradientDescentOptimizer(0.01)\n",
    "train = optim.minimize(loss,name='train')\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    init = tf.global_variables_initializer()\n",
    "    sess.run(init)\n",
    "\n",
    "print ('W:',sess.run(W),'b:',sess.run(b),'loss: ',sess.run(loss))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "update step:  0 W: [ 0.09491654] b: [ 0.00646906] loss:  0.100477\n",
      "update step:  1 W: [ 0.09798094] b: [ 0.01277907] loss:  0.0956369\n",
      "update step:  2 W: [ 0.10083736] b: [ 0.01893521] loss:  0.0911051\n",
      "update step:  3 W: [ 0.10349743] b: [ 0.02494244] loss:  0.0868568\n",
      "update step:  4 W: [ 0.10597219] b: [ 0.0308055] loss:  0.0828694\n",
      "update step:  5 W: [ 0.10827205] b: [ 0.03652895] loss:  0.0791223\n",
      "update step:  6 W: [ 0.11040691] b: [ 0.04211717] loss:  0.0755969\n",
      "update step:  7 W: [ 0.11238612] b: [ 0.04757434] loss:  0.0722763\n",
      "update step:  8 W: [ 0.11421853] b: [ 0.05290449] loss:  0.069145\n",
      "update step:  9 W: [ 0.1159125] b: [ 0.0581115] loss:  0.0661889\n",
      "update step:  10 W: [ 0.11747599] b: [ 0.06319907] loss:  0.0633952\n",
      "update step:  11 W: [ 0.1189165] b: [ 0.06817076] loss:  0.0607523\n",
      "update step:  12 W: [ 0.12024114] b: [ 0.07303002] loss:  0.0582493\n",
      "update step:  13 W: [ 0.12145662] b: [ 0.07778013] loss:  0.0558766\n",
      "update step:  14 W: [ 0.12256931] b: [ 0.08242426] loss:  0.0536253\n",
      "update step:  15 W: [ 0.12358524] b: [ 0.08696546] loss:  0.0514872\n",
      "update step:  16 W: [ 0.1245101] b: [ 0.09140666] loss:  0.0494547\n",
      "update step:  17 W: [ 0.12534928] b: [ 0.09575068] loss:  0.0475212\n",
      "update step:  18 W: [ 0.12610789] b: [ 0.10000025] loss:  0.0456801\n",
      "update step:  19 W: [ 0.12679075] b: [ 0.10415797] loss:  0.0439258\n",
      "update step:  20 W: [ 0.12740241] b: [ 0.10822637] loss:  0.0422529\n",
      "update step:  21 W: [ 0.1279472] b: [ 0.11220789] loss:  0.0406566\n",
      "update step:  22 W: [ 0.12842919] b: [ 0.11610485] loss:  0.0391322\n",
      "update step:  23 W: [ 0.12885223] b: [ 0.11991952] loss:  0.0376755\n",
      "update step:  24 W: [ 0.12921999] b: [ 0.12365408] loss:  0.0362828\n",
      "update step:  25 W: [ 0.1295359] b: [ 0.12731063] loss:  0.0349505\n",
      "update step:  26 W: [ 0.12980323] b: [ 0.13089119] loss:  0.0336752\n",
      "update step:  27 W: [ 0.13002504] b: [ 0.13439773] loss:  0.0324538\n",
      "update step:  28 W: [ 0.13020426] b: [ 0.13783213] loss:  0.0312835\n",
      "update step:  29 W: [ 0.13034365] b: [ 0.14119624] loss:  0.0301616\n",
      "update step:  30 W: [ 0.13044578] b: [ 0.14449179] loss:  0.0290857\n",
      "update step:  31 W: [ 0.13051312] b: [ 0.14772052] loss:  0.0280534\n",
      "update step:  32 W: [ 0.13054799] b: [ 0.15088406] loss:  0.0270626\n",
      "update step:  33 W: [ 0.13055256] b: [ 0.15398401] loss:  0.0261112\n",
      "update step:  34 W: [ 0.13052893] b: [ 0.15702192] loss:  0.0251975\n",
      "update step:  35 W: [ 0.13047902] b: [ 0.1599993] loss:  0.0243195\n",
      "update step:  36 W: [ 0.13040468] b: [ 0.16291757] loss:  0.0234756\n",
      "update step:  37 W: [ 0.13030766] b: [ 0.16577815] loss:  0.0226643\n",
      "update step:  38 W: [ 0.13018958] b: [ 0.16858239] loss:  0.0218842\n",
      "update step:  39 W: [ 0.130052] b: [ 0.17133161] loss:  0.0211337\n",
      "update step:  40 W: [ 0.12989637] b: [ 0.1740271] loss:  0.0204117\n",
      "update step:  41 W: [ 0.12972407] b: [ 0.17667007] loss:  0.0197168\n",
      "update step:  42 W: [ 0.12953639] b: [ 0.17926174] loss:  0.019048\n",
      "update step:  43 W: [ 0.12933455] b: [ 0.18180329] loss:  0.0184041\n",
      "update step:  44 W: [ 0.12911971] b: [ 0.18429582] loss:  0.017784\n",
      "update step:  45 W: [ 0.12889294] b: [ 0.18674043] loss:  0.0171869\n",
      "update step:  46 W: [ 0.12865528] b: [ 0.1891382] loss:  0.0166117\n",
      "update step:  47 W: [ 0.12840769] b: [ 0.19149017] loss:  0.0160575\n",
      "update step:  48 W: [ 0.12815104] b: [ 0.19379734] loss:  0.0155236\n",
      "update step:  49 W: [ 0.12788622] b: [ 0.19606067] loss:  0.0150091\n",
      "update step:  50 W: [ 0.12761401] b: [ 0.19828112] loss:  0.0145132\n",
      "update step:  51 W: [ 0.12733515] b: [ 0.20045963] loss:  0.0140352\n",
      "update step:  52 W: [ 0.12705036] b: [ 0.20259708] loss:  0.0135744\n",
      "update step:  53 W: [ 0.1267603] b: [ 0.20469436] loss:  0.0131302\n",
      "update step:  54 W: [ 0.1264656] b: [ 0.20675232] loss:  0.0127019\n",
      "update step:  55 W: [ 0.12616684] b: [ 0.20877177] loss:  0.0122889\n",
      "update step:  56 W: [ 0.12586455] b: [ 0.21075353] loss:  0.0118907\n",
      "update step:  57 W: [ 0.12555927] b: [ 0.21269839] loss:  0.0115066\n",
      "update step:  58 W: [ 0.12525147] b: [ 0.2146071] loss:  0.0111361\n",
      "update step:  59 W: [ 0.1249416] b: [ 0.21648043] loss:  0.0107788\n",
      "update step:  60 W: [ 0.12463008] b: [ 0.21831909] loss:  0.0104341\n",
      "update step:  61 W: [ 0.1243173] b: [ 0.22012378] loss:  0.0101016\n",
      "update step:  62 W: [ 0.12400364] b: [ 0.2218952] loss:  0.00978089\n",
      "update step:  63 W: [ 0.12368944] b: [ 0.22363403] loss:  0.00947144\n",
      "update step:  64 W: [ 0.12337502] b: [ 0.22534092] loss:  0.00917288\n",
      "update step:  65 W: [ 0.12306068] b: [ 0.22701651] loss:  0.00888482\n",
      "update step:  66 W: [ 0.1227467] b: [ 0.22866143] loss:  0.00860687\n",
      "update step:  67 W: [ 0.12243333] b: [ 0.23027629] loss:  0.00833866\n",
      "update step:  68 W: [ 0.12212083] b: [ 0.23186168] loss:  0.00807986\n",
      "update step:  69 W: [ 0.1218094] b: [ 0.23341818] loss:  0.00783011\n",
      "update step:  70 W: [ 0.12149926] b: [ 0.23494637] loss:  0.0075891\n",
      "update step:  71 W: [ 0.12119061] b: [ 0.2364468] loss:  0.00735652\n",
      "update step:  72 W: [ 0.12088361] b: [ 0.23792] loss:  0.00713205\n",
      "update step:  73 W: [ 0.12057845] b: [ 0.2393665] loss:  0.00691543\n",
      "update step:  74 W: [ 0.12027525] b: [ 0.24078684] loss:  0.00670636\n",
      "update step:  75 W: [ 0.11997417] b: [ 0.24218149] loss:  0.00650457\n",
      "update step:  76 W: [ 0.11967532] b: [ 0.24355099] loss:  0.00630982\n",
      "update step:  77 W: [ 0.11937884] b: [ 0.24489579] loss:  0.00612184\n",
      "update step:  78 W: [ 0.11908483] b: [ 0.24621637] loss:  0.00594041\n",
      "update step:  79 W: [ 0.11879337] b: [ 0.24751319] loss:  0.00576529\n",
      "update step:  80 W: [ 0.11850456] b: [ 0.2487867] loss:  0.00559626\n",
      "update step:  81 W: [ 0.11821849] b: [ 0.25003737] loss:  0.0054331\n",
      "update step:  82 W: [ 0.11793522] b: [ 0.25126559] loss:  0.00527562\n",
      "update step:  83 W: [ 0.11765482] b: [ 0.2524718] loss:  0.0051236\n",
      "update step:  84 W: [ 0.11737733] b: [ 0.25365645] loss:  0.00497686\n",
      "update step:  85 W: [ 0.11710282] b: [ 0.2548199] loss:  0.00483522\n",
      "update step:  86 W: [ 0.11683134] b: [ 0.25596255] loss:  0.00469849\n",
      "update step:  87 W: [ 0.11656291] b: [ 0.25708479] loss:  0.00456651\n",
      "update step:  88 W: [ 0.11629757] b: [ 0.25818703] loss:  0.0044391\n",
      "update step:  89 W: [ 0.11603536] b: [ 0.2592696] loss:  0.00431611\n",
      "update step:  90 W: [ 0.11577629] b: [ 0.26033288] loss:  0.00419739\n",
      "update step:  91 W: [ 0.11552038] b: [ 0.26137725] loss:  0.00408278\n",
      "update step:  92 W: [ 0.11526766] b: [ 0.26240304] loss:  0.00397214\n",
      "update step:  93 W: [ 0.11501812] b: [ 0.2634106] loss:  0.00386534\n",
      "update step:  94 W: [ 0.11477178] b: [ 0.26440027] loss:  0.00376224\n",
      "update step:  95 W: [ 0.11452864] b: [ 0.26537237] loss:  0.0036627\n",
      "update step:  96 W: [ 0.1142887] b: [ 0.2663272] loss:  0.00356662\n",
      "update step:  97 W: [ 0.11405197] b: [ 0.26726511] loss:  0.00347387\n",
      "update step:  98 W: [ 0.11381843] b: [ 0.26818642] loss:  0.00338432\n",
      "update step:  99 W: [ 0.11358808] b: [ 0.2690914] loss:  0.00329788\n"
     ]
    }
   ],
   "source": [
    "for step in range(100):\n",
    "    sess.run(train)\n",
    "    print ('update step: ',step,'W:',sess.run(W),'b:',sess.run(b),'loss: ',sess.run(loss))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
